import docker
from llm_sandbox import SandboxSession, SandboxBackend
from llm_sandbox.docker import SandboxDockerSession

from app.core.logging import logger

# Connect to remote Docker daemon 远程连接，暂时没有成功，测试时总是显示连接被拒绝
# client = docker.DockerClient(
#     base_url="tcp://127.0.0.1:2376",
#     timeout=30
# )

# 方式1 啥都自己创建临时的
# with SandboxSession(lang="python",# 语言
#                     backend=SandboxBackend.DOCKER,# 使用docker容器 默认也是这个
#                     verbose=True # 过程日志
#                     ) as session:
#     result = session.run("""
# print("Hello from LLM Sandbox!")
# print("你好 llm sandbox")
#     """)
#
# print(result.stdout)

# 方式2 指定本地docker镜像
# with SandboxDockerSession(lang="python",verbose=True,
#                           image="python:3.12",  # 使用自定义镜像
#                           ) as session:
#     result = session.run("""
# print("Hello from LLM Sandbox!")
# print("你好 llm sandbox")
#     """)
#
#     print(result.stdout)



# 方式3 指定本地docker容器
# client = docker.from_env()
# # Create and use a sandbox session
# with SandboxSession(lang="python",
#                     backend=SandboxBackend.DOCKER,
#                     verbose=True,
#                     container_id="my_python_container",
#                     client=client,
#     skip_environment_setup=True
#
#                     ) as session:
#     result = session.run("""
# print("Hello from LLM Sandbox!")
# print("你好 llm sandbox")
#     """)
#
#     print(result.stdout)

# 4 容器中手动添加包numpy测试
client = docker.from_env()
# Create and use a sandbox session
with SandboxSession(lang="python",
                    backend=SandboxBackend.DOCKER,
                    verbose=True,
                    container_id="my_python_container",
                    client=client,
    skip_environment_setup=True

                    ) as session:
    result = session.run("""
import numpy as np

# Create an array
arr = np.array([1, 2, 3, 4, 5])
print(f"Array: {arr}")
print(f"Mean: {np.mean(arr)}")
print(f"Sum: {np.sum(arr)}")
    """)

    print(result.stdout)